// window.addEventListener('refundListEvent', function(event) {
// 	var detail = event.detail;
// 	setTimeout(function() {
// 		pullupRefresh()
// 	}, 500)

// });


var mask = null;
var count = 1
var login_user = null

$(function() { // 初始化内容

	mask = mui.createMask(); //遮罩层
	mui.init();
	tap_init();
	mui('#pullrefresh').pullRefresh({
		up: { //上拉加载
			height: 50, //可选.默认50.触发上拉加载拖动距离
			auto: true, //可选,默认false.自动上拉加载一次
			contentrefresh: "正在加载...",
			contentnomore: '没有更多数据了',
			callback: function() { //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
				window.setTimeout(function() {
					pullupRefresh()
				}, 500);
			}
		}

	})
	login_user = JSON.parse(localStorage.getItem('loginUser'));
	datetime_init();


})

function pullupRefresh() {

	var status = $('#status option:selected').val();
	loadAuditReccord(status)
}


function tap_init() {

	jQuery(document).ready(function() {

		jQuery("#status").change(function() {
			mui('#pullrefresh').pullRefresh().refresh(true);
			mui('#pullrefresh').pullRefresh().enablePullupToRefresh(true)
			var status = $('#status option:selected').val();
			app.todos = []

			count = 1
			loadAuditReccord(status)
		});

	});
	mui(".mui-scroll").on("tap", ".mui-control-item", function(e) {
		var id = this.getAttribute("data_id");
		console.log(id)

		console.log("跳转")
		window.sessionStorage.setItem("refundid", id)
		window.location.href = "refund_form.html"
	});

}



function loadAuditReccord(status) {

	console.log(status)
	var data = {};

	data['page'] = count;
	data["rows"] = 20;
	data['dqjlauditstatus'] = status;
	data['type'] = "退款";
	data['createdatestart'] = "2018-01-01";
	data['createdateend'] = endCurrentMonth1();
	data['searchrole'] = login_user.role;
	console.log("退款记录=====" + JSON.stringify(data));
	jQuery.ajax({
		type: "post",
		headers: {
			'Authorization': login_user.token
		},
		url: serverip + "dqjl/tkspGrid",
		contentType: 'application/json;charset=UTF-8',
		data: JSON.stringify(data),
		dataType: "json",
		timeout: 10000,
		beforeSend: function() {

			mask.show(); //显示遮罩层	
		},
		success: function(res) {
			count = count + 1

			mask.close(); //显示遮罩层 	  
			console.log("记录列表=====" + JSON.stringify(res));
			var recordList = res.tkList;
			loadApp(recordList, status);

			var num = Number(res.total)
			//数据加载完成后执行的操作,隐藏加载页面,显示数据
			mui('#pullrefresh').pullRefresh().endPullupToRefresh(true); //停止正在加载
			mui('#pullrefresh').pullRefresh().enablePullupToRefresh(true); //启用上拉加载


		},
		error: function(e) {
			console.log("错误信息====" + JSON.stringify(e));
			mask.close(); //显示遮罩层
			var msg = e.responseJSON.message;
			var code = e.responseJSON.status;
			mui.alert('错误编码' + code + "\n" + "错误信息:" + msg);
			console.log("错误信息====" + JSON.stringify(e));
		}
	});



}


var app = new Vue({
	el: '#app',
	data: {
		show: true,
		todos: []
	},
	methods: {
		// selected: function(todo) {

		// 	console.log("跳转")
		// 	window.sessionStorage.setItem("refundid", JSON.stringify(todo.id))
		// 	window.location.href = "refund_form.html"

		// }
	}
});

function loadApp(data, status) {
	console.log("data1====" + JSON.stringify(data));

	for (var i = 0; i < data.length; i++) {
		var d = data[i];
		var o = new Object();
		for (var x in d) {
			o[x] = d[x];

			switch (d.dqjlauditstatus) {
				case "审核中":
					o.imgsrc = '../../img/waitingimg.png';
					break;
				case "大区经理审核通过":
					o.imgsrc = '../../img/successimg.png'
					break;
				case "大区经理审核不通过":
					o.imgsrc = '../../img/errorimg.png'
					break;
				default:
					break;
			}
		}

		app.todos.push(o);
	}

}
